<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item :hidden="isDoAdd" label="id" prop="id">
        <el-input
          v-model="form.id"
          autocomplete="off"
          :disabled="!isDoAdd"
        ></el-input>
      </el-form-item>

      <el-form-item label="类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择">
          <el-option
            v-for="(item, V) in typeOptions"
            :key="V"
            :label="item"
            :value="V"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="题干" prop="detail">
        <el-input
          v-model="form.detail"
          autocomplete="off"
          type="textarea"
          :autosize="{ minRows: 3 }"
        ></el-input>
      </el-form-item>
      <el-form-item label="难度" prop="level">
        <el-select v-model="form.level" placeholder="请选择">
          <el-option
            v-for="item in levelOptions"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标签" prop="tags">
        <el-input v-model="form.tags" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="分数" prop="score">
        <el-input v-model="form.score" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="解析" prop="resolve">
        <el-input
          v-model="form.resolve"
          autocomplete="off"
          type="textarea"
          :autosize="{ minRows: 1 }"
        ></el-input>
      </el-form-item>
      <el-form-item
        v-if="form.type == 'Completion' || form.type == 'FreeResponse'"
        label="文本答案"
        prop="remark"
      >
        <el-input
          v-model="form.answerContent"
          autocomplete="off"
          type="textarea"
          :autosize="{ minRows: 1 }"
        ></el-input>
      </el-form-item>

      <el-form-item
        v-if="form.type == 'Single' || form.type == 'Multi'"
        label="选项"
        prop="remark"
      >
        <el-table :show-header="false" :data="options" style="width: 100%">
          <el-table-column
            prop="content"
            label=""
            width="140"
          ></el-table-column>
          <el-table-column prop="correct" label="" width="70">
            <template #default="{ row }">
              <el-button
                v-if="!row.correct"
                size="small"
                icon="el-icon-close"
                @click="setToCorrect(true, row.id)"
              ></el-button>
              <el-button
                v-if="row.correct"
                type="success"
                size="small"
                icon="el-icon-check"
                @click="setToCorrect(false, row.id)"
              ></el-button>
            </template>
          </el-table-column>

          <el-table-column prop="correct" label="" width="150">
            <template #default="{ row }">
              <el-button-group>
                <el-button
                  size="small"
                  icon="el-icon-upload2"
                  circle
                  @click="del(row.id)"
                ></el-button>
                <el-button
                  size="small"
                  icon="el-icon-download"
                  circle
                  @click="del(row.id)"
                ></el-button>
                <el-button
                  size="small"
                  icon="el-icon-delete"
                  circle
                  @click="del(row.id)"
                ></el-button>
              </el-button-group>
            </template>
          </el-table-column>
        </el-table>
        <el-button @click="addOption()">加项</el-button>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doEdit, doAdd, loadQueOptions } from '@/api/questionManagement'
  import { QuestionType } from '../../../data/question'

  export default {
    name: 'ManagementEdit',
    props: {
      idKey: {
        type: String,
        default: function () {
          return 'id'
        },
      },
    },
    data() {
      return {
        typeOptions: QuestionType,
        options: [],
        levelOptions: ['初级', '中级', '高级'],
        form: {
          id: '',
          sort: 1,
        },
        rules: {
          displayName: [{ required: true, trigger: 'blur', message: '请输入' }],
        },
        title: '',
        dialogFormVisible: false,
        isDoAdd: false,
      }
    },
    created() {},
    methods: {
      addOption() {},
      del(id) {},
      async loadOptions(id) {
        let data = await loadQueOptions(id)
        console.log(data)
        this.options = data.data
      },
      showEdit(row) {
        if (!row[this.idKey]) {
          this.title = '添加'
          this.isDoAdd = true
          this.options = []
        } else {
          this.title = '编辑'
          this.isDoAdd = false

          this.form = Object.assign({}, row)
          this.loadOptions(row[this.idKey])
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            let _msg = ''
            if (this.isDoAdd) {
              const { msg } = await doAdd(this.form)
              _msg = msg
            } else {
              const { msg } = await doEdit(this.form)
              _msg = msg
            }
            this.$baseMessage(_msg, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
